package com.atguigu.tingshu.live.api;

import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.live.config.LiveConfig;
import com.atguigu.tingshu.live.service.LiveRoomService;
import com.atguigu.tingshu.model.live.LiveRoom;
import com.atguigu.tingshu.vo.live.LiveRoomVo;
import com.atguigu.tingshu.vo.live.TencentLiveAddressVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/live/liveRoom")
@SuppressWarnings({"unchecked", "rawtypes"})
public class LiveRoomApiController {

	@Autowired
	private LiveRoomService liveRoomService;
	@Autowired
	private LiveConfig liveConfig;
	///api/live/liveRoom/findLiveList
	@Operation(summary = "用户查询所有在线的直播间")
	@GetMapping("findLiveList")
	public Result findLiveList(){
		List<LiveRoom> liveRooms = liveRoomService.list(Wrappers.lambdaQuery(LiveRoom.class)
				.gt(LiveRoom::getExpireTime, new Date())
				.eq(LiveRoom::getStatus, "1"));
		return Result.ok(liveRooms);
	}
/*
{"code":200,"message":"成功","data":{"id":6,
"createTime":"2025-04-24 10:32:18","userId":12,
"coverUrl":"http://192.168.123.100:9000/tingshu/2025/04-24/1915231916567592960.jpg",
"liveTitle":"hehe","viewCount":0,"status":"1","tagId":"4","appName":"live",
"streamName":"12","expireTime":"2025-04-25 10:30:55",
"pushUrl":"webrtc://209208.push.tlivecloud.com/live/12?txSecret=7c1d7922501f420ac0e0f6d9dcb88dcc&txTime=680A1332",
"playUrl":"webrtc://www.coffee-part.com/live/12?txSecret=7c1d7922501f420ac0e0f6d9dcb88dcc&txTime=680A1332","longitude":104.072750,"latitude":30.578990,"location":"北京市立水桥"}}


 */
	///api/live/liveRoom/getById/6
	@Operation(summary = "查询直播间详情")
	@GetMapping("getById/{roomId}")
	@GuiguLogin
	public Result getById(@PathVariable("roomId")Long roomId){
		return Result.ok(liveRoomService.getById(roomId));
	}
	///api/live/liveRoom/saveLiveRoom
	@Operation(summary = "保存直播间")
	@PostMapping("saveLiveRoom")
	@GuiguLogin
	public Result saveLiveRoom(@RequestBody LiveRoomVo liveRoomVo){
		LiveRoom liveRoom = new LiveRoom();
		BeanUtils.copyProperties(liveRoomVo , liveRoom);
		liveRoom.setStatus("1");
		liveRoom.setUserId(AuthContextHolder.getUserId());
		liveRoom.setViewCount(0);
		liveRoom.setAppName(liveConfig.getAppName());
		String streamName = AuthContextHolder.getUserId().toString();
		liveRoom.setStreamName(streamName);
		TencentLiveAddressVo liveRoomUrl = liveConfig.getLiveRoomUrl(streamName);
		liveRoom.setPushUrl(liveRoomUrl.getPushWebRtcUrl());
		liveRoom.setPlayUrl(liveRoomUrl.getPullWebRtcUrl());
		liveRoomService.save(liveRoom);
		return Result.ok(liveRoom);
	}
	///api/live/liveRoom/getCurrentLive
	@Operation(summary = "获取用户直播间")
	@GetMapping("getCurrentLive")
	@GuiguLogin
	public Result getCurrentLive(){
		LiveRoom liveRoom = liveRoomService.getOne(Wrappers.lambdaQuery(LiveRoom.class)
				.eq(LiveRoom::getUserId, AuthContextHolder.getUserId())
				.gt(LiveRoom::getExpireTime, new Date()));
		return Result.ok(liveRoom);
	}

}

